<?php

namespace app\admin\model\tongcheng;

use addons\tongcheng\model\BaseModel;


class PayLogModel extends BaseModel
{
    // 表名
    protected $name = 'tongcheng_pay_log';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'status_text',
        'error_time_text',
        'pay_time_text',
        'payable_url'
    ];

    public function getPayableUrlAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['payable_type']) ? $data['payable_type'] : '');
        $list  = [
            DeliveryOrderModel::class                         => 'tongcheng/delivery_order/index',
            \addons\tongcheng\model\DeliveryOrderModel::class => 'tongcheng/delivery_order/index',
            // 'app\admin\model\tongcheng\order\WithdrawModel'   => 'tongcheng/rider_withdraw/index',
        ];
        return isset($list[$value]) ? url($list[$value], ['id' => $data['payable_id']]) : '';
    }


    public function getStatusList()
    {
        return ['0' => __('Status 0'), '1' => __('Status 1')];
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list  = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getErrorTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['error_time']) ? $data['error_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getPayTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['pay_time']) ? $data['pay_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setErrorTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setPayTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


    public function user()
    {
        return $this->belongsTo('app\admin\model\User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function zone()
    {
        return $this->belongsTo('Zone', 'zone_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    /**
     * 多态关联变动对象
     * @return \think\model\relation\MorphTo
     */
    public function payable()
    {
        return $this->morphTo();
    }
}
